-- slime_vertical_damage
-- create by zhuangsl
-- 技能：对一列怪物造成伤害
-- 配置：slime_vertical_damage(10, 600, 200)：冈布奥专属技能10，伤害 = attack * (0.6 + 0.2 * level)

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 专属技能等级
        local level;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            local prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        -- 威力
        local base = para[2];
        local force = para[3];

        -- 攻击方的攻击
        local attack = source:getAttack();

        -- 伤害
        local damage = math.modf(attack * (base + force * level) / 1000);

        -- 斯巴达专属加成
        local prop = PropM.combine(source, "battle_cry", 1);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end
        damage = PropM.apply(prop, damage);
        
        -- 连携附带暴击效果
        prop = PropM.combine(source, "even_carrying", "crit");
        if FormulaM.invoke("CAN_EVEN_CARRYING", skillId) and prop[3] > 0 then
            damage = damage + PropM.trigger(source, prop[1], prop[2], damage);
        end

        -- 4. 遍历怪物所在列
        local ret = {};
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end

        local coor = DungeonM.convertToCoor(pos);
        local y;
        local m;

        local cover = { };
        for y = 1, DUNGEON_HEIGHT do
            m = DungeonM.convertToPos(coor.x, y);
            table.insert(cover, m);
        end

        for _, m in pairs(cover) do
            local grid = DungeonM.getGridByPos(m);

            -- 如果格子已经翻开，且是未死亡的怪物
            if  grid:isOpened() and grid:isMonster() and
                not grid.monster:isDead() then

                -- 增加命中动作
                if SkillM.getSequence(grid.monster) == nil then
                    SkillM.getSequenceList()[grid.monster] = CastSequence.create(source, grid.monster, skillId);
                end

                SkillM.getSequence(grid.monster):hit(source, grid.monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, grid.monster, damage, skillId, extra);
                
                -- 连携附带粉碎效果
                prop = PropM.combine(source, "even_carrying", "smash");
                if FormulaM.invoke("CAN_EVEN_CARRYING", skillId) and prop[3] ~= 0 and not grid.monster:isDead() then
                    local effectRound = round + prop[4] + 1;
                    local condition = {
                        ["value"] = prop[3],
                        ["end_round"] = effectRound,
                    };

                    CombatStatusM.applyStatus(grid.monster, prop[2], condition);
                end

                -- 目标
                table.insert(ret, grid.monster);
            end
        end


        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]) };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 专属技能等级
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = PropM.apply(prop, 0);

        -- 威力
        local base = para[2];
        local force = para[3];

        -- 攻击方的攻击
        local attack = source:getAttack();

        -- 伤害
        local damage = math.modf(attack * (base + force * level) / 1000);

        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);
        damage = PropM.apply(prop, damage);

        desc = string.gsub(desc, "{damage}", damage);

        return desc;
    end,
};
